﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using Doo.WebUI;
using Doo.Caching;
using System.ComponentModel.Design.Serialization;

namespace Doo.Controls
{
    /*
     * 版本号管理器
     */

    [ToolboxData("<{0}:VersionManager runat=server></{0}:VersionManager>")]
    [ParseChildren(ChildrenAsProperties = true)]
    public class VersionManager : Control
    {
        #region 枚举定义

        public enum VersionType
        {
            /// <summary>
            /// 附加在query上
            /// </summary>
            Query = 0,
            /// <summary>
            /// 使用etag
            /// </summary>
            ETag = 1
        }

        #endregion

        /// <summary>
        /// 使用版本号行为，默认为附加查询语句
        /// </summary>
        [DefaultValue(VersionType.Query)]
        public VersionType UseVersionType { get; set; }

        /// <summary>
        /// 检查版本号的间隔，为0则不检查
        /// </summary>
        public int CheckVersionTimer { get; set; }

        /// <summary>
        /// 服务端依赖
        /// </summary>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public List<VersionKey> ServerKeys { get; set; }

        /// <summary>
        /// 客户端依赖
        /// </summary>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public List<VersionKey> ClientKeys { get; set; }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            //向ContentCache中注册
            ContentCache cache = ContentCache.Current;
            cache.EnableCache = true;
            if (ServerKeys != null)
                foreach (VersionKey k in ServerKeys)
                {
                    cache.AddServerCacheKey(k.DynamicKeys);
                }
            if (ClientKeys != null)
                foreach (VersionKey k in ClientKeys)
                {
                    cache.AddClientCacheKey(k.DynamicKeys);
                }
        }
    }

    [TypeConverter(typeof(VersionKeyTypeConverter))]
    public class VersionKey
    {
        /// <summary>
        /// 动态键
        /// </summary>
        [TypeConverter(typeof(StringArrayConverter))]
        public string[] DynamicKeys { get; set; }

        /// <summary>
        /// 获取实际键
        /// </summary>
        /// <returns></returns>
        public string GetKey()
        {
            return PageCacheHelper.GetKey(DynamicKeys);
        }

        public override int GetHashCode()
        {
            if (DynamicKeys == null) return 0;
            return DynamicKeys.GetHashCode();
        }

        public static VersionKey Parse(string k)
        {
            VersionKey vk = new VersionKey();
            vk.DynamicKeys = k.Split(',');
            return vk;
        }

        public override string ToString()
        {
            return string.Join(",", DynamicKeys);
        }
    }

    public class VersionKeyTypeConverter : TypeConverter
    {
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is string && value != null)
            {
                return VersionKey.Parse((string)value);
            }
            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(InstanceDescriptor))
            {
                VersionKey ce = (VersionKey)value;
                return new InstanceDescriptor(typeof(VersionKey).GetMethod("Parse"),
                    new object[] { ce.ToString() }
                );
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}
